<?php

namespace app\user\model\user;

use app\common\helper\util\Tool;
use app\common\model\BaseModel;
use eduline\upload\Client as FileClient;
use eduline\upload\utils\Util;
use think\facade\Request;
use think\model\concern\SoftDelete;

/**
 * 附件数据库定义
 */
class FacePic extends BaseModel
{
    /**
     * 使用软删除
     */
    use SoftDelete;

    /**
     * 表名
     * @var string
     */
    protected $name = 'user_face_pic';

    /**
     * 定义删除的字段
     *
     * @var string
     */
    protected $deleteTime = 'delete_time';
    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 不写入更新时间
     *
     * @var boolean
     */
    protected $updateTime = false;
    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i:s',
        'delete_time' => 'timestamp:Y-m-d H:i:s',
        'id'          => 'integer',
    ];

    /**
     * 设置扩展类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2019-11-12
     *
     * @param string $extension 扩展类型
     * @param array  $data      源数据
     */
    protected function setExtensionAttr($extension, $data)
    {
        if (!$extension) {
            $extension = strtolower(substr(strrchr($data['savename'], '.'), 1));
        }

        return $extension;
    }

    /**
     * 文件大小转换
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2019-11-12
     *
     * @param    [type]                         $size [description]
     * @return   [type]                               [description]
     */
    protected function getFilesizeAttr($size)
    {
        $arr = ["B", "KB", "MB", "GB", "TB", "PB"];
        $pos = 0;
        while ($size >= 1024) {
            $size /= 1024;
            $pos++;
        }
        return round($size, 2) . " " . $arr[$pos];

    }

    /**
     * 获取文件的保存地址
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2019-11-13
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    protected function getFilepathAttr($value, $data)
    {
        $file = new FileClient('local');

        return $file->path($data);
    }

    /**
     * 获取文件的访问url
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2019-11-13
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    protected function getFileurlAttr($value, $data)
    {

        $file = new FileClient('local');

        return $file->url($data);
    }

}
